﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using KorleBu.Application_Classes;

namespace KorleBu
{
    public partial class home : System.Web.UI.Page
    {
        #region Class State Variables

        /// <summary>
        /// Dataset object, used for housing login method calls results.
        /// </summary>
        DataSet ds = new DataSet();
        /// <summary>
        /// Presentation object that accesses the presentation object classes methods.
        /// Used for accessing business methods for the logging out.
        /// </summary>
        Presentation presentation = new Presentation();

        #endregion

        ////Jason Vance 10-31-12; Lazy initialize the label found on MasterPage.Master
        //private Label _lblMasterError;
        //private Label lblMasterError
        //{
        //    get
        //    {
        //        if (_lblMasterError == null)
        //            _lblMasterError = (Label)Master.FindControl("lblMasterError");
        //        return _lblMasterError;
        //    }
        //}

        #region Methods

        protected void Page_Load(object sender, EventArgs e)
        {
            //if the user didn't log in, but browsed to this page redirect them to the login page.
            if (GetSessionID() == null || GetSessionID() == "")
            {
                Response.Redirect("~/Login.aspx", true);
            }

            lblLoggedInUser.Text = "You are logged in as " + GetFirstName() + " " + GetLastName();
            if (!IsPostBack)
            {
                //show a message for pending tasks if the given user has tasks to do.  
                //This should only apply to store managers, general managers, CEOs, and Procurement
                if (GetIsStoreManager())
                {
                    ds = presentation.GetPendingRequests(GetStoreID());
                    if (ds.Tables.Count > 0) // If the method returned successfully
                    {
                        if (ds.Tables[0].Rows[0][0].ToString() != "") // if errors have occurred
                        {
                            //lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString(); // Error message
                        }
                        else
                        {
                            //lblSideBarContent.Text = "**You have pending satellite store requests to handle.**";
                        }
                    }
                }
                else if (GetIsGeneralManager())
                {
                    ds = presentation.GetRequisitionsByPendingStatus();
                    if (ds.Tables.Count > 0) // If the method returned successfully
                    {
                        if (ds.Tables[0].Rows[0][0].ToString() != "") // if errors have occurred
                        {
                            //lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString(); // Error message
                        }
                        else
                        {
                            //lblSideBarContent.Text = "**You have pending requisitions to handle.**";
                        }
                    }
                }
                else if (GetIsCEO())
                {
                    ds = presentation.GetRequisitionsByGMStatus();
                    if (ds.Tables.Count > 0) // If the method returned successfully
                    {
                        if (ds.Tables[0].Rows[0][0].ToString() != "") // if errors have occurred
                        {
                            //lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString(); // Error message
                        }
                        else
                        {
                            //lblSideBarContent.Text = "**You have pending requisitions to handle.**";
                        }
                    }
                }
                else if (GetIsProcurement())
                {
                    ds = presentation.GetRequisitionsByCEOStatus();
                    if (ds.Tables.Count > 0) // If the method returned successfully
                    {
                        if (ds.Tables[0].Rows[0][0].ToString() != "") // if errors have occurred
                        {
                            //lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString(); // Error message
                        }
                        else
                        {
                            //lblSideBarContent.Text = "**You have pending requisition orders to handle.**";
                        }
                    }
                }
            }
        }
        #endregion

        #region Session Methods
        private int GetStoreID()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getStoreId();
        }
        private string GetSessionID()
        {
            try
            {
                User lg = (User)Session["LoggedInUser"];
                return lg.getSessionId();
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
                return null;
            }
        }
        private string GetFirstName()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getFirstName();
        }
        private string GetLastName()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getLastName();
        }
        private bool GetIsAdmin()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getIsAdmin();
        }
        private bool GetIsGeneralManager()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getIsGeneralManager();
        }
        private bool GetIsStoreManager()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getIsStoreManager();
        }
        private bool GetIsStoreKeeper()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getIsStoreKeeper();
        }
        private bool GetIsSubUnitClerk()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getIsSubUnitClerk();
        }
        private bool GetIsAccounting()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getIsAccounting();
        }
        private bool GetIsProcurement()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getIsProcurement();
        }
        private bool GetIsCEO()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getIsCEO();
        }
        #endregion
    }
}